﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Security.Cryptography;
using System.Text;
using DataBase;

namespace DBService
{
	public class DbUser : DbObject
	{
		public string Name
		{
			get
			{
				return this.name;
			}
			set
			{
				this.name = value;
			}
		}

		public string Password
		{
			get
			{
				return this.password;
			}
			set
			{
				this.password = value;
			}
		}

		public string Email
		{
			get
			{
				return this.email;
			}
			set
			{
				this.email = value;
			}
		}

		public string ChineseName
		{
			get
			{
				return this.chineseName;
			}
			set
			{
				this.chineseName = value;
			}
		}

		public string Remark
		{
			get
			{
				return this.remark;
			}
			set
			{
				this.remark = value;
			}
		}

		public UserStatus Status
		{
			get
			{
				return this.staus;
			}
		}

		public DbUser(string uName, string uEmail, string uPassword, string uChineseName)
		{
			this.name = uName;
			this.email = uEmail;
			this.password = uPassword;
			this.chineseName = uChineseName;
		}

		public DbUser(int uid, string uName, string uEmail, string uPassword, string uChineseName, UserStatus us, IDatabase udb)
		{
			this.mId = uid;
			this.name = uName;
			this.email = uEmail;
			this.password = uPassword;
			this.chineseName = uChineseName;
			this.mDatabase = udb;
			this.staus = us;
		}

		public int GetCount()
		{
			int result;
			try
			{
				if (this.mDatabase.Connection.State == ConnectionState.Closed)
				{
					this.mDatabase.Connection.Open();
				}
				string sql = string.Format("select count from tbuser where id={0}", this.mId);
				DataTable dataTable = this.mDatabase.ExecQuery(sql);
				if (this.mDatabase.Connection.State == ConnectionState.Open)
				{
					this.mDatabase.Connection.Close();
				}
				if (dataTable != null && dataTable.Rows.Count > 0)
				{
					result = Convert.ToInt32(dataTable.Rows[0]["count"].ToString());
				}
				else
				{
					result = 0;
				}
			}
			catch (Exception ex)
			{
				if (this.mDatabase.Connection.State == ConnectionState.Open)
				{
					this.mDatabase.Connection.Close();
				}
				throw ex;
			}
			return result;
		}

		public bool Rename(string newName)
		{
			throw new NotImplementedException();
		}

		public DateTime GetLastTime()
		{
			throw new NotImplementedException();
		}

		public DateTime GetCreateTime()
		{
			throw new NotImplementedException();
		}

		public static DbUser GetUser(string uName, IDatabase db)
		{
			DbUser result;
			try
			{
				if (db.Connection.State == ConnectionState.Closed)
				{
					db.Connection.Open();
				}
				string sql = string.Format("select Id, Name,Password,ChineseName,Email,Status,Remark from tbUSER where Name='{0}'", uName);
				DataTable dataTable = db.ExecQuery(sql);
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				if (dataTable != null && dataTable.Rows.Count > 0)
				{
					DbUser dbUser = new DbUser(Convert.ToInt32(dataTable.Rows[0]["Id"].ToString()), dataTable.Rows[0]["Name"].ToString(), dataTable.Rows[0]["Email"].ToString(), dataTable.Rows[0]["Password"].ToString().Trim(), dataTable.Rows[0]["ChineseName"].ToString().Trim(), (UserStatus)dataTable.Rows[0]["Status"], db);
					string text = dataTable.Rows[0]["Remark"].ToString();
					dbUser.remark = text;
					result = dbUser;
				}
				else
				{
					result = null;
				}
			}
			catch (Exception)
			{
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				result = null;
			}
			return result;
		}

		public static DbUser GetFromDB(int id, IDatabase db)
		{
			DbUser result;
			try
			{
				if (db.Connection.State == ConnectionState.Closed)
				{
					db.Connection.Open();
				}
				string sql = string.Format("select Id, Name,Password,ChineseName,Email,Status,Remark from tbUSER where Id={0}", id);
				DataTable dataTable = db.ExecQuery(sql);
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				if (dataTable != null && dataTable.Rows.Count > 0)
				{
					DbUser dbUser = new DbUser(Convert.ToInt32(dataTable.Rows[0]["Id"].ToString().Trim()), dataTable.Rows[0]["Name"].ToString().Trim(), dataTable.Rows[0]["Email"].ToString().Trim(), dataTable.Rows[0]["Password"].ToString().Trim(), dataTable.Rows[0]["ChineseName"].ToString().Trim(), (UserStatus)dataTable.Rows[0]["Status"], db);
					string text = dataTable.Rows[0]["Remark"].ToString();
					dbUser.remark = text;
					result = dbUser;
				}
				else
				{
					result = null;
				}
			}
			catch (Exception ex)
			{
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				throw ex;
			}
			return result;
		}

		public void SubmitToProject(int projectId, UserType type, IDatabase db)
		{
			try
			{
				if (db.Connection.State == ConnectionState.Closed)
				{
					db.Connection.Open();
				}
				string sql = string.Format("INSERT INTO tbProjectUserRelation (ProjectId,UserId,UserType,Valid) VALUES ({0},{1},{2},{3})", new object[]
				{
					projectId,
					base.Id,
					(int)type,
					0
				});
				db.ExecuteCommand(sql);
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				this.mDatabase = db;
			}
			catch (Exception ex)
			{
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				throw ex;
			}
		}

		public void Verify()
		{
			try
			{
				if (this.mDatabase.Connection.State == ConnectionState.Closed)
				{
					this.mDatabase.Connection.Open();
				}
				string sql = string.Format("update tbUSER set Status=1 where Id={0}", this.mId);
				this.mDatabase.ExecuteCommand(sql);
				if (this.mDatabase.Connection.State == ConnectionState.Open)
				{
					this.mDatabase.Connection.Close();
				}
			}
			catch (Exception ex)
			{
				if (this.mDatabase.Connection.State == ConnectionState.Open)
				{
					this.mDatabase.Connection.Close();
				}
				throw ex;
			}
		}

		public void Approval(UserType type, DbProject project)
		{
			try
			{
				if (this.mDatabase.Connection.State == ConnectionState.Closed)
				{
					this.mDatabase.Connection.Open();
				}
				string sql = string.Format("update tbProjectUserRelation set Valid=1 where UserId={0} and ProjectId={1} and UserType={2}", this.mId, project.Id, (int)type);
				this.mDatabase.ExecuteCommand(sql);
				if (this.mDatabase.Connection.State == ConnectionState.Open)
				{
					this.mDatabase.Connection.Close();
				}
			}
			catch (Exception ex)
			{
				if (this.mDatabase.Connection.State == ConnectionState.Open)
				{
					this.mDatabase.Connection.Close();
				}
				throw ex;
			}
		}

		public List<DbRole> GetRoles()
		{
			List<DbRole> result;
			try
			{
				if (this.mDatabase.Connection.State == ConnectionState.Closed)
				{
					this.mDatabase.Connection.Open();
				}
				List<DbRole> list = new List<DbRole>();
				string sql = string.Format("SELECT r.Id,Name,Description FROM tbRole AS r INNER JOIN tbUserRoleRelation AS ur ON r.Id=ur.RoleId where ur.UserId={0}", this.mId);
				DataTable dataTable = this.mDatabase.ExecQuery(sql);
				if (this.mDatabase.Connection.State == ConnectionState.Open)
				{
					this.mDatabase.Connection.Close();
				}
				if (dataTable != null)
				{
					for (int i = 0; i < dataTable.Rows.Count; i++)
					{
						list.Add(new DbRole(Convert.ToInt32(dataTable.Rows[i]["Id"]), dataTable.Rows[i]["Name"].ToString(), dataTable.Rows[i]["Description"].ToString(), this.mDatabase));
					}
				}
				result = list;
			}
			catch (Exception ex)
			{
				if (this.mDatabase.Connection.State == ConnectionState.Open)
				{
					this.mDatabase.Connection.Close();
				}
				throw ex;
			}
			return result;
		}

		public override void AddToDb(IDatabase db)
		{
			try
			{
				if (db.Connection.State == ConnectionState.Closed)
				{
					db.Connection.Open();
				}
				string sql = string.Format("INSERT INTO tbUSER (Name,Password,ChineseName,Email,Status,Remark) VALUES ('{0}','{1}','{2}','{3}',{4},'{5}')", new object[]
				{
					this.name,
					this.password,
					this.chineseName,
					this.email,
					1,
					this.remark
				});
				db.ExecuteCommand(sql);
				sql = "select max(Id) from tbUSER";
				DataTable dataTable = db.ExecQuery(sql);
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				if (dataTable.Rows.Count > 0)
				{
					this.mId = Convert.ToInt32(dataTable.Rows[0][0]);
				}
				this.mDatabase = db;
			}
			catch (Exception ex)
			{
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				throw ex;
			}
		}

		public static string ToMd5(string pw)
		{
			HashAlgorithm hashAlgorithm = new MD5CryptoServiceProvider();
			byte[] bytes = Encoding.Default.GetBytes(pw);
			return BitConverter.ToString(hashAlgorithm.ComputeHash(bytes)).Replace("-", "");
		}

		public void Unregister()
		{
			try
			{
				if (this.mDatabase.Connection.State == ConnectionState.Closed)
				{
					this.mDatabase.Connection.Open();
				}
				string sql = string.Format("UPDATE tbUSER SET Status = 0 WHERE Id = {0}", this.mId);
				this.mDatabase.ExecuteCommand(sql);
				if (this.mDatabase.Connection.State == ConnectionState.Open)
				{
					this.mDatabase.Connection.Close();
				}
			}
			catch (Exception ex)
			{
				if (this.mDatabase.Connection.State == ConnectionState.Open)
				{
					this.mDatabase.Connection.Close();
				}
				throw ex;
			}
		}

		public void UpdateRole(List<DbRole> roleList)
		{
			try
			{
				if (this.mDatabase.Connection.State == ConnectionState.Closed)
				{
					this.mDatabase.Connection.Open();
				}
				string sql = string.Format("DELETE FROM tbUserRoleRelation WHERE UserId = {0}", this.mId);
				this.mDatabase.ExecuteCommand(sql);
				foreach (DbRole dbRole in roleList)
				{
					string sql2 = string.Format("insert into tbUserRoleRelation (UserId,RoleId) VALUES ({0},{1})", this.mId, dbRole.Id);
					this.mDatabase.ExecuteCommand(sql2);
				}
				if (this.mDatabase.Connection.State == ConnectionState.Open)
				{
					this.mDatabase.Connection.Close();
				}
			}
			catch (Exception ex)
			{
				if (this.mDatabase.Connection.State == ConnectionState.Open)
				{
					this.mDatabase.Connection.Close();
				}
				throw ex;
			}
		}

		public void UpdateRight(DbUser user, List<DbRight> brightList, List<DbRight> rightList)
		{
			this.RemoveAllRight(user, brightList);
			foreach (DbRight right in rightList)
			{
				user.AddRight(right);
			}
		}

		public void RemoveAllRight(DbUser user, List<DbRight> brightList)
		{
			try
			{
				if (this.mDatabase.Connection.State == ConnectionState.Closed)
				{
					this.mDatabase.Connection.Open();
				}
				foreach (DbRight dbRight in brightList)
				{
					string sql = string.Format("DELETE FROM tbUserRightRelation WHERE UserId = {0} AND RightId = {1}", user.Id, dbRight.Id);
					this.mDatabase.ExecuteCommand(sql);
				}
				if (this.mDatabase.Connection.State == ConnectionState.Open)
				{
					this.mDatabase.Connection.Close();
				}
			}
			catch (Exception ex)
			{
				if (this.mDatabase.Connection.State == ConnectionState.Open)
				{
					this.mDatabase.Connection.Close();
				}
				throw ex;
			}
		}

		public void AddRight(DbRight right)
		{
			try
			{
				if (this.mDatabase.Connection.State == ConnectionState.Closed)
				{
					this.mDatabase.Connection.Open();
				}
				string sql = string.Format("insert into tbUserRightRelation (UserId,RightId) VALUES ({0},{1})", this.mId, right.Id);
				this.mDatabase.ExecuteCommand(sql);
				if (this.mDatabase.Connection.State == ConnectionState.Open)
				{
					this.mDatabase.Connection.Close();
				}
			}
			catch
			{
				if (this.mDatabase.Connection.State == ConnectionState.Open)
				{
					this.mDatabase.Connection.Close();
				}
			}
		}

		public DbDepartment Department
		{
			get
			{
				DbDepartment result = null;
				try
				{
					if (this.mDatabase.Connection.State == ConnectionState.Closed)
					{
						this.mDatabase.Connection.Open();
					}
					string sql = string.Format("select DepartmentId from tbDepartmentUserRelation where UserId={0}", base.Id);
					DataTable dataTable = this.mDatabase.ExecQuery(sql);
					if (this.mDatabase.Connection.State == ConnectionState.Open)
					{
						this.mDatabase.Connection.Close();
					}
					if (dataTable != null && dataTable.Rows.Count > 0)
					{
						int id = Convert.ToInt32(dataTable.Rows[0]["DepartmentId"]);
						result = DbDepartment.GetDepartment(this.mDatabase, id);
					}
				}
				catch (Exception ex)
				{
					if (this.mDatabase.Connection.State == ConnectionState.Open)
					{
						this.mDatabase.Connection.Close();
					}
					throw ex;
				}
				return result;
			}
		}

		public List<DbRight> GetRightList()
		{
			List<DbRight> list = new List<DbRight>();
			List<DbRight> result;
			try
			{
				if (this.mDatabase.Connection.State == ConnectionState.Closed)
				{
					this.mDatabase.Connection.Open();
				}
				string sql = string.Format("select Id,UserId, RightId from tbUserRightRelation where UserId={0}", base.Id);
				DataTable dataTable = this.mDatabase.ExecQuery(sql);
				if (this.mDatabase.Connection.State == ConnectionState.Open)
				{
					this.mDatabase.Connection.Close();
				}
				if (dataTable != null)
				{
					for (int i = 0; i < dataTable.Rows.Count; i++)
					{
						int id = Convert.ToInt32(dataTable.Rows[i]["RightId"]);
						DbRight right = DbRight.GetRight(this.mDatabase, id);
						if (right != null)
						{
							list.Add(right);
						}
					}
				}
				result = list;
			}
			catch (Exception ex)
			{
				if (this.mDatabase.Connection.State == ConnectionState.Open)
				{
					this.mDatabase.Connection.Close();
				}
				throw ex;
			}
			return result;
		}

		public List<DbRight> GetRightList(string group)
		{
			List<DbRight> list = new List<DbRight>();
			List<DbRight> result;
			try
			{
				if (this.mDatabase.Connection.State == ConnectionState.Closed)
				{
					this.mDatabase.Connection.Open();
				}
				string sql = string.Format("select Id,UserId, RightId from tbUserRightRelation where UserId={0}", base.Id);
				DataTable dataTable = this.mDatabase.ExecQuery(sql);
				if (this.mDatabase.Connection.State == ConnectionState.Open)
				{
					this.mDatabase.Connection.Close();
				}
				if (dataTable != null)
				{
					for (int i = 0; i < dataTable.Rows.Count; i++)
					{
						int id = Convert.ToInt32(dataTable.Rows[i]["RightId"]);
						DbRight right = DbRight.GetRight(this.mDatabase, id);
						if (right != null)
						{
							list.Add(right);
						}
					}
				}
				result = list;
			}
			catch (Exception ex)
			{
				if (this.mDatabase.Connection.State == ConnectionState.Open)
				{
					this.mDatabase.Connection.Close();
				}
				throw ex;
			}
			return result;
		}

		public void UpdateRole(List<DbRole> roleList, string group)
		{
			try
			{
				if (this.mDatabase.Connection.State == ConnectionState.Closed)
				{
					this.mDatabase.Connection.Open();
				}
				string sql = string.Format("SELECT tbUserRoleRelation.Id FROM tbUserRoleRelation INNER JOIN tbRole ON tbUserRoleRelation.RoleId = tbRole.Id WHERE (tbUserRoleRelation.UserId ={0}) AND (tbRole.RoleGroup = '{1}')", this.mId, group);
				DataTable dataTable = this.mDatabase.ExecQuery(sql);
				if (dataTable != null && dataTable.Rows.Count > 0)
				{
					for (int i = 0; i < dataTable.Rows.Count; i++)
					{
						string sql2 = string.Format("DELETE FROM tbUserRoleRelation WHERE Id = {0}", dataTable.Rows[i][0].ToString());
						this.mDatabase.ExecuteCommand(sql2);
					}
				}
				foreach (DbRole dbRole in roleList)
				{
					string sql3 = string.Format("insert into tbUserRoleRelation (UserId,RoleId) VALUES ({0},{1})", this.mId, dbRole.Id);
					this.mDatabase.ExecuteCommand(sql3);
				}
				if (this.mDatabase.Connection.State == ConnectionState.Open)
				{
					this.mDatabase.Connection.Close();
				}
			}
			catch (Exception ex)
			{
				if (this.mDatabase.Connection.State == ConnectionState.Open)
				{
					this.mDatabase.Connection.Close();
				}
				throw ex;
			}
		}

		public override void UpdateToDb()
		{
			try
			{
				if (base.Database.Connection.State == ConnectionState.Closed)
				{
					base.Database.Connection.Open();
				}
				string sql = string.Format("UPDATE tbUSER SET Password = '{0}' ,Name='{1}', Email='{2}',Remark='{3}'  WHERE Id ={4}", new object[]
				{
					this.password,
					this.name,
					this.email,
					this.remark,
					base.Id
				});
				base.Database.ExecuteCommand(sql);
				if (base.Database.Connection.State == ConnectionState.Open)
				{
					base.Database.Connection.Close();
				}
			}
			catch (Exception ex)
			{
				if (base.Database.Connection.State == ConnectionState.Open)
				{
					base.Database.Connection.Close();
				}
				throw ex;
			}
		}

		public void ChangePassword(string newPassword)
		{
			try
			{
				if (base.Database.Connection.State == ConnectionState.Closed)
				{
					base.Database.Connection.Open();
				}
				string sql = string.Format("UPDATE tbUSER SET Password = '{0}' WHERE Id ={1}", DbUser.ToMd5(newPassword), base.Id);
				base.Database.ExecuteCommand(sql);
				if (base.Database.Connection.State == ConnectionState.Open)
				{
					base.Database.Connection.Close();
				}
			}
			catch (Exception ex)
			{
				if (base.Database.Connection.State == ConnectionState.Open)
				{
					base.Database.Connection.Close();
				}
				throw ex;
			}
		}

		public override void RemoveFromDb()
		{
			try
			{
				if (base.Database.Connection.State == ConnectionState.Closed)
				{
					base.Database.Connection.Open();
				}
				string sql = string.Format("DELETE FROM tbUSER WHERE Id = {0}", base.Id);
				base.Database.ExecuteCommand(sql);
				if (base.Database.Connection.State == ConnectionState.Closed)
				{
					base.Database.Connection.Open();
				}
				this.RemoveFromDepartment();
				foreach (DbUserMessage dbUserMessage in this.GetMessage())
				{
					dbUserMessage.RemoveFromDb();
				}
				if (this.mDatabase.Connection.State == ConnectionState.Closed)
				{
					this.mDatabase.Connection.Open();
				}
				try
				{
					string sql2 = string.Format("DELETE FROM tbUserRoleRelation WHERE UserId = {0}", base.Id);
					this.mDatabase.ExecuteCommand(sql2);
				}
				catch (Exception ex)
				{
					if (this.mDatabase.Connection.State == ConnectionState.Open)
					{
						this.mDatabase.Connection.Close();
					}
					throw ex;
				}
				try
				{
					string sql3 = string.Format("DELETE FROM tbUserRightRelation WHERE UserId = {0}", base.Id);
					this.mDatabase.ExecuteCommand(sql3);
				}
				catch (Exception ex2)
				{
					if (this.mDatabase.Connection.State == ConnectionState.Open)
					{
						this.mDatabase.Connection.Close();
					}
					throw ex2;
				}
				if (this.mDatabase.Connection.State == ConnectionState.Open)
				{
					this.mDatabase.Connection.Close();
				}
			}
			catch (Exception ex3)
			{
				if (base.Database.Connection.State == ConnectionState.Open)
				{
					base.Database.Connection.Close();
				}
				if (this.mDatabase.Connection.State == ConnectionState.Open)
				{
					this.mDatabase.Connection.Close();
				}
				throw ex3;
			}
		}

		public bool IsAdmin()
		{
			bool result = false;
			using (List<DbRight>.Enumerator enumerator = this.GetRightList("SYS").GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					if (enumerator.Current.Name.Trim() == "系统管理")
					{
						result = true;
						break;
					}
				}
			}
			return result;
		}

		public bool IsFamilyManager()
		{
			bool result = false;
			using (List<DbRight>.Enumerator enumerator = this.GetRightList().GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					if (enumerator.Current.Name.Trim() == "族库管理")
					{
						result = true;
						break;
					}
				}
			}
			return result;
		}

		public bool CanAddFamily()
		{
			bool result = false;
			using (List<DbRight>.Enumerator enumerator = this.GetRightList("FM").GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					if (enumerator.Current.Name.Trim() == "添加族")
					{
						result = true;
						break;
					}
				}
			}
			return result;
		}

		public bool CanCheckFamily()
		{
			bool result = false;
			using (List<DbRight>.Enumerator enumerator = this.GetRightList("FM").GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					if (enumerator.Current.Name.Trim() == "审核族")
					{
						result = true;
						break;
					}
				}
			}
			return result;
		}

		public bool CanAddType()
		{
			bool result = false;
			using (List<DbRight>.Enumerator enumerator = this.GetRightList("FM").GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					if (enumerator.Current.Name.Trim() == "添加规格")
					{
						result = true;
						break;
					}
				}
			}
			return result;
		}

		public bool CanAddCategory()
		{
			bool result = false;
			using (List<DbRight>.Enumerator enumerator = this.GetRightList("FM").GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					if (enumerator.Current.Name.Trim() == "添加分类")
					{
						result = true;
						break;
					}
				}
			}
			return result;
		}

		public bool CanDelFamily()
		{
			bool result = false;
			using (List<DbRight>.Enumerator enumerator = this.GetRightList("FM").GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					if (enumerator.Current.Name.Trim() == "删除族")
					{
						result = true;
						break;
					}
				}
			}
			return result;
		}

		public bool CanDelType()
		{
			bool result = false;
			using (List<DbRight>.Enumerator enumerator = this.GetRightList("FM").GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					if (enumerator.Current.Name.Trim() == "删除规格")
					{
						result = true;
						break;
					}
				}
			}
			return result;
		}

		public bool CanDelCategory()
		{
			bool result = false;
			using (List<DbRight>.Enumerator enumerator = this.GetRightList("FM").GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					if (enumerator.Current.Name.Trim() == "删除分类")
					{
						result = true;
						break;
					}
				}
			}
			return result;
		}

		public bool CanEditFamily()
		{
			bool result = false;
			using (List<DbRight>.Enumerator enumerator = this.GetRightList("FM").GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					if (enumerator.Current.Name.Trim() == "编辑族")
					{
						result = true;
						break;
					}
				}
			}
			return result;
		}

		public bool CanEditType()
		{
			bool result = false;
			using (List<DbRight>.Enumerator enumerator = this.GetRightList("FM").GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					if (enumerator.Current.Name.Trim() == "编辑规格")
					{
						result = true;
						break;
					}
				}
			}
			return result;
		}

		public bool CanEditCategory()
		{
			bool result = false;
			using (List<DbRight>.Enumerator enumerator = this.GetRightList("FM").GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					if (enumerator.Current.Name.Trim() == "编辑分类")
					{
						result = true;
						break;
					}
				}
			}
			return result;
		}

		public bool CanImport()
		{
			bool result = false;
			using (List<DbRight>.Enumerator enumerator = this.GetRightList("FM").GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					if (enumerator.Current.Name.Trim() == "导入")
					{
						result = true;
						break;
					}
				}
			}
			return result;
		}

		public bool CanExport()
		{
			bool result = false;
			using (List<DbRight>.Enumerator enumerator = this.GetRightList("FM").GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					if (enumerator.Current.Name.Trim() == "导出")
					{
						result = true;
						break;
					}
				}
			}
			return result;
		}

		public bool IsProjectManager()
		{
			bool result = false;
			using (List<DbRight>.Enumerator enumerator = this.GetRightList().GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					if (enumerator.Current.Name.Trim() == "项目管理")
					{
						result = true;
						break;
					}
				}
			}
			return result;
		}

		public override string ToString()
		{
			return this.name;
		}

		public static List<DbUser> GetAllUsers(IDatabase db)
		{
			List<DbUser> result;
			try
			{
				if (db.Connection.State == ConnectionState.Closed)
				{
					db.Connection.Open();
				}
				List<DbUser> list = new List<DbUser>();
				string sql = string.Format("select Id, Name,Password,ChineseName,Email,Status,Remark from TBUSER", Array.Empty<object>());
				DataTable dataTable = db.ExecQuery(sql);
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				if (dataTable != null)
				{
					for (int i = 0; i < dataTable.Rows.Count; i++)
					{
						DbUser dbUser = new DbUser(Convert.ToInt32(dataTable.Rows[i]["Id"].ToString()), dataTable.Rows[i]["Name"].ToString(), dataTable.Rows[i]["Email"].ToString(), dataTable.Rows[i]["Password"].ToString(), dataTable.Rows[i]["ChineseName"].ToString().Trim(), (UserStatus)dataTable.Rows[i]["Status"], db);
						string text = dataTable.Rows[i]["Remark"].ToString();
						dbUser.remark = text;
						list.Add(dbUser);
					}
				}
				result = list;
			}
			catch (Exception ex)
			{
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				throw ex;
			}
			return result;
		}

		public void SendMessage(string userName, string message)
		{
			try
			{
				DbUser user = DbUser.GetUser(userName, this.mDatabase);
				if (user == null)
				{
					throw new Exception("用户不存在。");
				}
				if (this.mDatabase.Connection.State == ConnectionState.Closed)
				{
					this.mDatabase.Connection.Open();
				}
				string sql = string.Format("insert into tbusermessage (SenderUid,ReceiverUid,Message,Status,Date) VALUES ({0},{1},'{2}',{3},'{4}')", new object[]
				{
					this.mId,
					user.Id,
					message,
					0,
					DateTime.Now.ToString()
				});
				this.mDatabase.ExecuteCommand(sql);
				if (this.mDatabase.Connection.State == ConnectionState.Open)
				{
					this.mDatabase.Connection.Close();
				}
			}
			catch (Exception ex)
			{
				if (this.mDatabase.Connection.State == ConnectionState.Open)
				{
					this.mDatabase.Connection.Close();
				}
				throw ex;
			}
		}

		public void IncreaseCount()
		{
			try
			{
				if (this.mDatabase.Connection.State == ConnectionState.Closed)
				{
					this.mDatabase.Connection.Open();
				}
				string sql = string.Format("update tbUSER set Count=Count+1 where id={0}", this.mId);
				this.mDatabase.ExecuteCommand(sql);
				if (this.mDatabase.Connection.State == ConnectionState.Open)
				{
					this.mDatabase.Connection.Close();
				}
			}
			catch (Exception ex)
			{
				if (this.mDatabase.Connection.State == ConnectionState.Open)
				{
					this.mDatabase.Connection.Close();
				}
				throw ex;
			}
		}

		public List<DbUserMessage> GetMessage()
		{
			List<DbUserMessage> result;
			try
			{
				if (this.mDatabase.Connection.State == ConnectionState.Closed)
				{
					this.mDatabase.Connection.Open();
				}
				List<DbUserMessage> list = new List<DbUserMessage>();
				string sql = string.Format("select Id,ReceiverUid,Date,SenderUid,Message,Status from tbusermessage where ReceiverUid={0}", this.mId);
				DataTable dataTable = this.mDatabase.ExecQuery(sql);
				if (this.mDatabase.Connection.State == ConnectionState.Open)
				{
					this.mDatabase.Connection.Close();
				}
				if (dataTable != null)
				{
					for (int i = 0; i < dataTable.Rows.Count; i++)
					{
						int id = Convert.ToInt32(dataTable.Rows[i]["Id"]);
						DbUser fromDB = DbUser.GetFromDB(Convert.ToInt32(dataTable.Rows[i]["ReceiverUid"].ToString()), this.mDatabase);
						DbUser fromDB2 = DbUser.GetFromDB(Convert.ToInt32(dataTable.Rows[i]["SenderUid"].ToString()), this.mDatabase);
						DateTime time = Convert.ToDateTime(dataTable.Rows[i]["Date"].ToString());
						string txt = dataTable.Rows[i]["Message"].ToString();
						ReadStatus status = (ReadStatus)Convert.ToInt32(dataTable.Rows[i]["Status"].ToString());
						DbUserMessage dbUserMessage = new DbUserMessage(id, fromDB, fromDB2, time, txt, status, this.mDatabase);
						dbUserMessage.UpdateToDb();
						list.Add(dbUserMessage);
					}
				}
				result = list;
			}
			catch (Exception ex)
			{
				if (this.mDatabase.Connection.State == ConnectionState.Open)
				{
					this.mDatabase.Connection.Close();
				}
				throw ex;
			}
			return result;
		}

		public int NewMessageCount
		{
			get
			{
				int result;
				try
				{
					if (this.mDatabase.Connection.State == ConnectionState.Closed)
					{
						this.mDatabase.Connection.Open();
					}
					string sql = string.Format("select * from tbusermessage where Status={0} AND ReceiverUid={1}", 0, base.Id);
					DataTable dataTable = this.mDatabase.ExecQuery(sql);
					if (this.mDatabase.Connection.State == ConnectionState.Open)
					{
						this.mDatabase.Connection.Close();
					}
					if (dataTable == null)
					{
						result = 0;
					}
					else
					{
						result = dataTable.Rows.Count;
					}
				}
				catch (Exception ex)
				{
					if (this.mDatabase.Connection.State == ConnectionState.Open)
					{
						this.mDatabase.Connection.Close();
					}
					throw ex;
				}
				return result;
			}
		}

		public static DbUser System
		{
			get
			{
				return DbUser.GetUser("SYSTEM", BimServerDB.Default);
			}
		}

		public void ToDepartment(int departmentId)
		{
			try
			{
				this.RemoveFromDepartment();
				if (this.mDatabase.Connection.State == ConnectionState.Closed)
				{
					this.mDatabase.Connection.Open();
				}
				string sql = string.Format("INSERT INTO tbDepartmentUserRelation (DepartmentId,UserId) VALUES ('{0}','{1}')", departmentId, this.mId);
				this.mDatabase.ExecuteCommand(sql);
				if (this.mDatabase.Connection.State == ConnectionState.Open)
				{
					this.mDatabase.Connection.Close();
				}
			}
			catch (Exception ex)
			{
				if (this.mDatabase.Connection.State == ConnectionState.Open)
				{
					this.mDatabase.Connection.Close();
				}
				throw ex;
			}
		}

		public void RemoveFromDepartment()
		{
			try
			{
				if (this.mDatabase.Connection.State == ConnectionState.Closed)
				{
					this.mDatabase.Connection.Open();
				}
				string sql = string.Format("DELETE FROM tbDepartmentUserRelation WHERE UserId = {0}", this.mId);
				this.mDatabase.ExecuteCommand(sql);
				if (this.mDatabase.Connection.State == ConnectionState.Open)
				{
					this.mDatabase.Connection.Close();
				}
			}
			catch (Exception ex)
			{
				if (this.mDatabase.Connection.State == ConnectionState.Open)
				{
					this.mDatabase.Connection.Close();
				}
				throw ex;
			}
		}

		public override bool Equals(object obj)
		{
			return obj is DbUser && ((DbUser)obj).Id == base.Id;
		}

		private string name;

		private string password;

		private string email;

		private string chineseName;

		private string remark;

		private UserStatus staus;
	}
}
